Reverse a line
Create a python script:
layer = iface.activeLayer()
caps = layer.dataProvider().capabilities()
if caps & QgsVectorDataProvider.ChangeGeometries:
features = layer.selectedFeatures()
for feature in features:
print(feature.id())
geom = feature.geometry()
line = geom.asPolyline()[::-1]
newGeom = QgsGeometry.fromPolyline(line)
layer.dataProvider().changeGeometryValues({ feature.id() : newGeom })
Select one or multiple features (lines) (not the information tool, the yellow selection tool) and run the script. When you unselect the feature, it will show as having the other direction.
Assign names to stops
In this scenario I have a graph with some edges that have a stop attribute
(attribute id 0). For those edges, I want to change the value of the attribute.
I use this script:
layer = iface.activeLayer()
stops = [feature.id() for feature in layer.getFeatures() if feature["stop"] != NULL]
names = ["Barnabe", "Andrijana", "Arwen", "Caitlin"]
caps = layer.dataProvider().capabilities()
if caps & QgsVectorDataProvider.ChangeAttributeValues:
for i,featureID in enumerate(stops):
attrs = { 0: names[i] }
layer.dataProvider().changeAttributeValues({ featureID : attrs })